var main;
$(document).on('click','.activable',function(){
		removeActive($(this).attr("activable"));
		$(this).addClass("active");
})
$(document).ready(function() {
	$(".adminm").children("a").click(function() {
		if ($(this).hasClass("leftopen")) {
			$('#leftmenu').sidebar('toggle');
			return;
		}
		if ($(this).hasClass("lmenu")) {
			requestMenu($(this).attr("url"));
			return;
		}
	});
	
	$("#home").click();
});

function removeActive(handle) {
	console.log(handle);
	$("."+handle).removeClass("active");
}

function requestMenu(url1) {
	$('#leftmenu').sidebar('hide');
	$.ajax({
		url : url1,
		async : true,
		success : function(result) {
			dealBack($.parseJSON(result));
		}
	});
}

function dealError(code) {
	message1 = code.message;
	message("错误信息", message1, "error");

}
function dealBack(code) {
	if (code.state == 0) {
		dealError(code);
		return;
	}
	$(".TopMenuItem").remove();
	var root = code.rootURL;
	var m = code.main;
	main = root + m;
	$.each(code.list, function(index, item) {
		var active = false;
		if (index == m) {
			active = true;
		}
		addMenuItem(root, index, item, active,"TSItem");
		addMenuItem(root, index, item, active,"DItem");
	});
	loadURL(main);
}
function addMenuItem(root, name, text, active,type) {
	var item = $("<a class='ui item "+type+" TopMenuItem activable' activable='TopMenuItem'></a>");
	if (active) {
		item.addClass("active");
	}
	item.attr("url", root + name);
	item.attr(name,"true");
	item.attr("urlname",name);
	item.text(text);
	item.click(function() {
		loadURL($(this).attr("url"));
	});
	if(type=="DItem"){
		$("#downmenu").prepend(item);
	}else{
		$("#sign").after(item);
	}
}

function loadURL(url1) {
	$(".ui.dimmer").addClass("active");
	$.ajax({
		url : url1,
		async : true,
		success : function(result) {
			$("#container").html(result);
			$(".ui.dimmer").removeClass("active");
		}
	});
	$(".ui.dropdown.item").click(function() {
		$('.ui.dropdown').dropdown();
	});
}
